﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NimLearning
{
    public class NimGame
    {
        private Tuple<Player, Player> players;
        private Player currentPlayer;
        private GameState currentState;
        private List<String> gameHistory;

        public NimGame(Player first, Player second, GameState game)
        {
            players = new Tuple<Player, Player>(first, second);
            currentPlayer = first;

            if (game == null)
            {
                game = new GameState();
            }

            currentState = game;

            gameHistory = new List<string>();

            this.Play();
        }

        private void Play()
        {
            while (!currentState.IsDone())
            {
                gameHistory.Add(currentState.Serialize());
                currentState.Print();
                currentState = currentPlayer.GetMove(currentState);
                AlternatePlayers();
            }
        }

        private void AlternatePlayers()
        {
            if (currentPlayer.Equals(players.Item1))
            {
                currentPlayer = players.Item2;
            }
            else
            {
                currentPlayer = players.Item1;
            }
        }
    }
}
